"""
添加/编辑扫货时间频率配置对话框
"""
from PyQt6.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QLabel, 
                            QPushButton, QTimeEdit, QSlider, QFormLayout)
from PyQt6.QtCore import Qt, QTime

class AddFrequencyConfigDialog(QDialog):
    """添加/编辑扫货时间频率配置对话框"""
    
    def __init__(self, parent=None, time_range=None, frequency=None):
        super().__init__(parent)
        self.setWindowTitle("添加时间频率配置")
        self.resize(400, 250)
        self.time_range = time_range
        self.frequency = frequency
        self.setup_ui()
        
    def setup_ui(self):
        """初始化UI"""
        layout = QVBoxLayout(self)
        
        # 表单布局
        form_layout = QFormLayout()
        form_layout.setSpacing(15)
        form_layout.setContentsMargins(10, 10, 10, 10)
        
        # 时间区间
        time_range_layout = QHBoxLayout()
        
        # 开始时间
        self.start_time_edit = QTimeEdit()
        self.start_time_edit.setDisplayFormat("HH:mm")
        self.start_time_edit.setStyleSheet("""
            QTimeEdit {
                padding: 5px;
                border: 1px solid #dcdfe6;
                border-radius: 4px;
                font-size: 14px;
                color: #606266;
                min-width: 120px;
            }
            QTimeEdit:focus {
                border-color: #409eff;
            }
        """)
        
        # 结束时间
        self.end_time_edit = QTimeEdit()
        self.end_time_edit.setDisplayFormat("HH:mm")
        self.end_time_edit.setStyleSheet(self.start_time_edit.styleSheet())
        
        # 分隔符
        separator_label = QLabel("-")
        separator_label.setStyleSheet("font-size: 14px; font-weight: bold;")
        
        time_range_layout.addWidget(self.start_time_edit)
        time_range_layout.addWidget(separator_label)
        time_range_layout.addWidget(self.end_time_edit)
        
        form_layout.addRow("时间区间:", time_range_layout)
        
        # 频率滑块
        self.frequency_slider = QSlider(Qt.Orientation.Horizontal)
        self.frequency_slider.setMinimum(0)
        self.frequency_slider.setMaximum(100)
        self.frequency_slider.setValue(50)  # 默认值50%
        self.frequency_slider.setStyleSheet("""
            QSlider::groove:horizontal {
                height: 8px;
                background: #e4e7ed;
                border-radius: 4px;
            }
            QSlider::handle:horizontal {
                width: 16px;
                margin: -4px 0;
                border-radius: 8px;
                background: #409eff;
            }
            QSlider::sub-page:horizontal {
                background: #409eff;
                border-radius: 4px;
            }
        """)
        
        # 频率标签
        self.frequency_label = QLabel("50%")
        self.frequency_label.setStyleSheet("font-size: 14px; color: #409eff; font-weight: bold;")
        
        # 频率值变化时更新标签
        self.frequency_slider.valueChanged.connect(self.update_frequency_label)
        
        frequency_layout = QHBoxLayout()
        frequency_layout.addWidget(self.frequency_slider)
        frequency_layout.addWidget(self.frequency_label)
        
        form_layout.addRow("扫货频率:", frequency_layout)
        
        layout.addLayout(form_layout)
        
        # 按钮区域
        button_layout = QHBoxLayout()
        button_layout.setContentsMargins(0, 20, 0, 0)
        
        # 确定按钮
        confirm_btn = QPushButton("确定")
        confirm_btn.setStyleSheet("""
            QPushButton {
                padding: 8px 16px;
                background-color: #409eff;
                color: white;
                border: none;
                border-radius: 4px;
                font-size: 14px;
                min-width: 80px;
            }
            QPushButton:hover {
                background-color: #66b1ff;
            }
        """)
        confirm_btn.clicked.connect(self.accept)
        
        # 取消按钮
        cancel_btn = QPushButton("取消")
        cancel_btn.setStyleSheet("""
            QPushButton {
                padding: 8px 16px;
                background-color: white;
                color: #606266;
                border: 1px solid #dcdfe6;
                border-radius: 4px;
                font-size: 14px;
                min-width: 80px;
            }
            QPushButton:hover {
                color: #409eff;
                border-color: #c6e2ff;
                background-color: #ecf5ff;
            }
        """)
        cancel_btn.clicked.connect(self.reject)
        
        button_layout.addStretch()
        button_layout.addWidget(confirm_btn)
        button_layout.addWidget(cancel_btn)
        
        layout.addLayout(button_layout)
        
        # 如果是编辑模式，设置初始值
        if self.time_range and self.frequency is not None:
            self.set_initial_values()
    
    def set_initial_values(self):
        """设置初始值"""
        self.setWindowTitle("编辑时间频率配置")
        
        # 设置时间区间
        start_time_str, end_time_str = self.time_range.split("-")
        start_time = QTime.fromString(start_time_str, "HH:mm")
        end_time = QTime.fromString(end_time_str, "HH:mm")
        
        self.start_time_edit.setTime(start_time)
        self.end_time_edit.setTime(end_time)
        
        # 设置频率
        frequency_value = int(self.frequency * 100)
        self.frequency_slider.setValue(frequency_value)
        self.frequency_label.setText(f"{frequency_value}%")
    
    def update_frequency_label(self, value):
        """更新频率标签"""
        self.frequency_label.setText(f"{value}%")
    
    def get_form_data(self):
        """获取表单数据"""
        start_time = self.start_time_edit.time().toString("HH:mm")
        end_time = self.end_time_edit.time().toString("HH:mm")
        time_range = f"{start_time}-{end_time}"
        
        frequency = self.frequency_slider.value() / 100.0
        
        return {
            "time_range": time_range,
            "frequency": frequency
        }
